package com.youlin.controller;

import com.youlin.dto.*;
import com.youlin.dto.res.AdminResDto;
import com.youlin.dto.res.EditAdminResDto;
import com.youlin.entities.BygAdmin;
import com.youlin.service.BygAdminService;
import com.youlin.untils.AdminUtil;
import com.youlin.untils.MyBeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 账号管理
 * @author ${author}
 * @since 2020-06-01
 */
@ApiSort(3)
@Api(tags = {"3 - 账号管理"})
@RestController
@RequestMapping("/bygAdmin")
@CrossOrigin
public class BygAdminController {

    @Autowired
    private BygAdminService bygAdminService;

    /**
     * 获取全部账号信息列表
     * @param baseReqDto
     * @return
     */
    @PostMapping("/findAllAdmins")
    @ApiOperationSort(value = 1)
    @ApiOperation(value = "1 - 获取全部账号信息列表")
    public PageDataResDto<List<AdminResDto>> findAllAdmins (@RequestBody PageReqDto baseReqDto) {
        PageInfo pageInfo = baseReqDto.getPageInfo();
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        return bygAdminService.findAllAdmins (currentAdmin, pageInfo);
    }

    /**
     * 新建/编辑单位登录账户信息
     * @param baseReqDto
     * @return
     */
    @PostMapping("/editAdmin")
    @ApiOperationSort(value = 2)
    @ApiOperation(value = "2 - 新建/编辑单位登录账户信息",
            notes = "响应参数：status-message = {1008-登录账号重复，1009-账号使用者姓名重复}")
    public BaseResDto editAdmin (@RequestBody BaseReqDto<EditAdminResDto> baseReqDto) {
        EditAdminResDto data = baseReqDto.getData();
        if (MyBeanUtil.isParamError(data)) return new BaseResDto(Status.PARAMETERERROR);
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        return bygAdminService.editAdmin (currentAdmin.getUnitId(), data);
    }

    /**
     * 校验当前账户是否有未完成的工作
     * @param baseReqDto
     * @return
     */
    @PostMapping("/checkAdmin")
    @ApiOperationSort(value = 3)
    @ApiOperation(value = "3 - 校验当前账户是否有未完成的工作", notes = "请求参数：data = 账户ID\n" +
            "响应参数：status-message = {1010-当前账号下有未完成的工作}")
    public BaseResDto checkAdmin (@RequestBody BaseReqDto<Integer> baseReqDto) {
        Integer adminId = baseReqDto.getData();
        if (adminId == null) return new BaseResDto(Status.PARAMETERERROR);
        return bygAdminService.checkAdmin (adminId);
    }

    /**
     * 停用/启用单位登录账户：取反
     * @param baseReqDto
     * @return
     */
    @PostMapping("/upDownAdmin")
    @ApiOperationSort(4)
    @ApiOperation(value = "4 - 停用/启用单位登录账户：取反", notes = "请求参数：data = 账户ID")
    public BaseResDto upDownAdmin (@RequestBody BaseReqDto<Integer> baseReqDto) {
        Integer adminId = baseReqDto.getData();
        if (adminId == null) return new BaseResDto(Status.PARAMETERERROR);
        return bygAdminService.upDownAdmin (adminId);
    }

}